package org.example.AI;

/**
 * @author: 默苍璃
 * @date: 2024-07-18 15:03
 */
public class MementoPatternDemo {
    public static void main(String[] args) {
        TextEditor textEditor = new TextEditor();
        TextEditorCareTaker careTaker = new TextEditorCareTaker();

        // 编辑文本
        textEditor.write("Hello, ");
        textEditor.write("world!");

        // 保存当前状态
        TextEditorMemento memento1 = textEditor.save();
        careTaker.saveMemento(memento1);

        // 继续编辑文本
        textEditor.write(" This is a backup.");

        // 再次保存当前状态
        TextEditorMemento memento2 = textEditor.save();
        careTaker.saveMemento(memento2);

        // 恢复到之前的状态
        TextEditorMemento savedMemento = careTaker.restoreMemento();
        textEditor.restore(savedMemento);

        System.out.println("Current text: " + textEditor.getText());

    }

}
